package com.zggk.iroad.main;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.xyz.tabitem.BottmTabItem;

import com.zggk.iroad.R;
import com.zggk.iroad.api.MyAppliCation;
import com.zggk.iroad.downdialog.DownBtnListener;
import com.zggk.iroad.downdialog.DownDialog;
import com.zggk.iroad.main.home.HomeFragment;
import com.zggk.iroad.main.me.MeFragment;
import com.zggk.iroad.mvp.MVPBaseActivity;
import com.zggk.iroad.util.WeiboDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MainActivity extends MVPBaseActivity<MainContract.View, MainPresenter> implements MainContract.View {


    @Bind(R.id.home)
    BottmTabItem home;
    @Bind(R.id.my)
    BottmTabItem my;
    @Bind(R.id.buttom_tab)
    LinearLayout buttomTab;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @Bind(R.id.content)
    RelativeLayout content;
    private HomeFragment homeFragment;
    private MeFragment meFragment;
    private BottmTabItem[] buttms;
    private DownDialog downDialogB;
    private DownDialog downDialogF;
    private Dialog mWeiboDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        buttms = new BottmTabItem[]{home, my};
        initFragment();
        mWeiboDialog = WeiboDialogUtils.createLoadingDialog(MainActivity.this, "加载中...");
        mPresenter.downDb(MyAppliCation.spUtils.getString("GydwId"));
    }

    @Override
    protected int getLayout() {
        return R.layout.act_main;
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        homeFragment = new HomeFragment();
        meFragment = new MeFragment();
        goToFragment(homeFragment);
    }

    @OnClick({R.id.home, R.id.my})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home:
                goToFragment(homeFragment);
                setButtom(0);
                break;
            case R.id.my:
                goToFragment(meFragment);
                setButtom(1);
                break;
        }
    }

    private Fragment mContent = null;

    /**
     * 修改显示的内容 不会重新加载
     **/
    public void goToFragment(Fragment to) {
        if (mContent != to) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                if (mContent != null)
                    transaction.hide(mContent).add(R.id.fragment_container, to).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
                else
                    transaction.add(R.id.fragment_container, to).commitAllowingStateLoss();
            } else {
                if (mContent != null)
                    transaction.hide(mContent).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
                else
                    transaction.show(to).commitAllowingStateLoss();
            }
            mContent = to;
        }
    }

    /**
     * 设置底部按钮显示
     */
    private void setButtom(int position) {
        for (int i = 0; i < buttms.length; i++) {
            if (position == i) {
                buttms[i].setSelectState(true);
            } else {
                buttms[i].setSelectState(false);
            }
        }
    }

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    public void getData() {
        WeiboDialogUtils.closeDialog(mWeiboDialog);
    }
}

